//引入 defineStore 用于创建 store
import { defineStore } from "pinia";
import { computed, ref } from "vue";

// 创建一个store用于存储count组件中的状态(数据)并暴露
/* export const useCountStore = defineStore("count", {
  // actions中放置的方法用于响应组件中的动作
  actions: {
    increment(value: number) {
      if (this.sum < 10) {
        this.sum += value; // this为当前store
      }
    },
  },
  // 共享数据
  state() {
    return {
      sum: 0,
      school: "sqxy",
      address: "商丘",
    };
  },
  getters: {
    // 写法一：使用state参数
    bigSum(state): number {// 可以接收一个参数state:当前store对象
      return state.sum * 10;
    },
    // 写法二：使用this
    upperSchool(): string {// this为当前store对象
      return this.school.toUpperCase();
    },
  },
}); */
export const useCountStore = defineStore("count", () => {
  // state
  const sum = ref(0);
  const school = ref("sqxy");
  const address = ref("商丘");

  // actions
  function increment(value: number) {
    if (sum.value < 10) {
      sum.value += value;
    }
  }

  // getters
  const bigSum = computed(() => {
    return sum.value * 10;
  });

  const upperSchool = computed(() => {
    return school.value.toUpperCase();
  });

  return { sum, school, address, increment, bigSum, upperSchool };
});
